package com.waa.gulimall.product.service.impl;

import com.waa.gulimall.product.dao.BrandDao;
import com.waa.gulimall.product.dao.CategoryDao;
import com.waa.gulimall.product.entity.BrandEntity;
import com.waa.gulimall.product.entity.CategoryEntity;
import com.waa.gulimall.product.service.BrandService;
import com.waa.gulimall.product.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.waa.gulimall.common.utils.PageUtils;
import com.waa.gulimall.common.utils.Query;

import com.waa.gulimall.product.dao.CategoryBrandRelationDao;
import com.waa.gulimall.product.entity.CategoryBrandRelationEntity;
import com.waa.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
     @Autowired
     private BrandDao brandDao;
     @Autowired
     private CategoryDao categoryDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> queryPage(Long brandId) {
        final QueryWrapper<CategoryBrandRelationEntity> categoryBrandRelationEntityQueryWrapper = new QueryWrapper<>();
        categoryBrandRelationEntityQueryWrapper.eq("brand_id",brandId);
        final List<CategoryBrandRelationEntity> list = this.list(categoryBrandRelationEntityQueryWrapper);
        return list;
    }


    //保存
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        final BrandEntity brandEntity = brandDao.selectById(categoryBrandRelation.getBrandId());
        final CategoryEntity categoryEntity = categoryDao.selectById(categoryBrandRelation.getCatelogId());
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        final List<CategoryBrandRelationEntity> relationEntities = this.baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        final List<Long> brandidList = relationEntities.stream().map(item -> {
            return item.getBrandId();
        }).collect(Collectors.toList());
        final List<BrandEntity> brandEntities = brandDao.selectBatchIds(brandidList);
        return brandEntities;
    }

}